<?php

/**
 * 双旦活动排行榜
 * @version 2017-12-15
 */
class rank_eggfight_auto_cache extends auto_cache {

    private $key = "rank:eggfight:";

    public function load($param) {

        $rank_name = strim($param['rank_name']);
        $cache_time = intval($param['cache_time']);
        $page = intval($param['page']);
        $page_size = intval($param['page_size']);
        $cache_time = $cache_time > 0 ? $cache_time : 1800;
        $page = $page > 0 ? $page : 1;
        $page_size = $page_size > 0 ? $page_size : 10;
        $limitSql = ' limit ' . (($page - 1) * $page_size) . "," . $page_size;
        $begin_time = strtotime('2017-12-24') - 28800; // 活动开始时间
        //$begin_time = strtotime('2017-12-15'); // 测试从2017-12-15开始
        $end_time = strtotime('2018-1-3') - 28800; // 活动结束时间
        $year = date('Y');
        $this->key .= $rank_name . '_' . $page;

        $key_bf = $this->key . '_bf';

        $list = $GLOBALS['cache']->get($this->key, true);

        //$time=NOW_TIME;
        //$sqlstmt="insert into fanwe_video_prop_stat_day (from_user_id, to_user_id) select from_user_id, to_user_id from fanwe_video_prop_201712 group by from_user_id, to_user_id";
        //$sqlstmt="update fanwe_video_prop_stat_day sd left join (select from_user_id, to_user_id, sum(total_ticket) as total_ticket, sum(total_diamonds) as total_diamonds from fanwe_video_prop_201712 where create_time > sd.update_time group by from_user_id, to_user_id) as p on sd.from_user_id=p.from_user_id, sd.to_user_id=p.to_user_id set sd.total_diamonds=p.total_diamonds, sd.total_ticket=p.total_ticket, sd.update_time=$time where sd.update_time<$time";
        $list = false;
        if ($list === false) {
            $is_ok = $GLOBALS['cache']->set_lock($this->key);
            if (!$is_ok) {
                $list = $GLOBALS['cache']->get($key_bf, true);
            } else {
                $list = array();
                switch ($rank_name) {
                    case 'podcast_egg':
                        $sqlstmt = "select count(vef.id) as total_num, vef.user_id, u.nick_name, u.head_image from fanwe_video_eggfight vef left join fanwe_user u on vef.user_id=u.id where vef.egg_status=2 group by vef.user_id order by total_num desc" . $limitSql;
                        //$sqlstmt = "select count(vef.id) as total_num, vef.user_id, u.nick_name, u.head_image from fanwe_video_eggfight vef left join fanwe_user u on vef.user_id=u.id group by vef.user_id order by total_num desc" . $limitSql;
                        $list = array();
                        $list1 = $GLOBALS['db']->getAll($sqlstmt);
                        foreach ($list1 as $row) {
                            if (intval($row['total_num']) < 1) {
                                continue;
                            }
                            $list[] = $row;
                        }
                        break;
                    case 'podcast_ticket':
                        $sqlstmt = "select sum(vp.total_ticket) as total_num, vp.to_user_id as user_id, u.nick_name, u.head_image from {table} vp left join fanwe_user u on vp.to_user_id=u.id where vp.create_time>=$begin_time and vp.create_time < $end_time group by vp.to_user_id order by total_num desc" . $limitSql;
                        //$sqlstmt = "select sum(vp.total_ticket) as total_num, vp.to_user_id as user_id, u.nick_name, u.head_image from {table} vp left join fanwe_user u on vp.to_user_id=u.id group by vp.to_user_id order by total_num desc" . $limitSql;
                        $list = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201712', $sqlstmt));
                        if ($year == 2018) {
                            $list2 = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201801', $sqlstmt));
                            $list = $this->combin_array_and_sort($list, $list2);
                        }
                        break;
                    case 'user_scissor': // scissors:剪刀, hammer:锤子
                        $sqlstmt1 = "select sum(vp.num) as total_num, vp.from_user_id as user_id, u.nick_name, u.head_image from {table} vp left join fanwe_user u on vp.from_user_id=u.id left join fanwe_prop p on vp.prop_id=p.id where vp.create_time>=$begin_time and vp.create_time < $end_time and p.anim_type='scissors' group by vp.from_user_id order by total_num desc" . $limitSql;
                        //$sqlstmt1 = "select sum(vp.num) as vp.total_num, vp.from_user_id as user_id, u.nick_name, u.head_image from {table} vp left join fanwe_user u on vp.to_user_id=u.id where vp.prop_id=75 group by vp.from_user_id order by total_num desc" . $limitSql;
                        $list1 = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201712', $sqlstmt1));
                        $sqlstmt2 = "select floor(sum(vp.num) / 3) as total_num, vp.from_user_id as user_id, u.nick_name, u.head_image from {table} vp left join fanwe_user u on vp.from_user_id=u.id left join fanwe_prop p on vp.prop_id=p.id where vp.create_time>=$begin_time and vp.create_time < $end_time and p.anim_type='hammer' group by vp.from_user_id order by total_num desc" . $limitSql;
                        $list2 = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201712', $sqlstmt2));
                        if ($year == 2018) {
                            $list3 = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201801', $sqlstmt1));
                            $list4 = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201801', $sqlstmt2));
                            $list = $this->combin_array_and_sort($list1, $list2, $list3, $list4);
                        } else {
                            $list = $this->combin_array_and_sort($list1, $list2);
                        }
                        break;
                    case 'user_diamond':
                        $sqlstmt = "select sum(vp.total_diamonds) as total_num, vp.from_user_id as user_id, u.nick_name, u.head_image from {table} vp left join fanwe_user u on vp.from_user_id=u.id where vp.create_time>=$begin_time and vp.create_time < $end_time group by vp.from_user_id order by total_num desc" . $limitSql;
                        //$sqlstmt = "select sum(vp.total_diamonds) as total_num, vp.from_user_id as user_id, u.nick_name, u.head_image from {table} vp left join fanwe_user u on vp.to_user_id=u.id group by vp.from_user_id order by total_num desc" . $limitSql;
                        $list = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201712', $sqlstmt));
                        if ($year == 2018) {
                            $list2 = $GLOBALS['db']->getAll(str_replace('{table}', 'fanwe_video_prop_201801', $sqlstmt));
                            $list = $this->combin_array_and_sort($list, $list2);
                        }
                        break;
                }
                $GLOBALS['cache']->set($this->key, $list, $cache_time, true); //缓存时间 1800秒 
                $GLOBALS['cache']->set($key_bf, $list, 86400, true); //备份
            }
        }
        if ($list) {
            for ($i = 0; $i < count($list); $i++) {
                $list[$i]['nick_name'] = emoji_decode($list[$i]['nick_name']);
                $list[$i]['head_image'] = get_spec_image($list[$i]['head_image']);
            }
        }
        return $list ? $list : array();
    }

    /**
     * 合并两个数组
     * @param type $list1
     * @param type $lists
     * @return type
     */
    function combin_array_and_sort($list1, ...$lists) {
        $list = array();
        if ($list1) {
            foreach ($list1 as $row) {
                if (intval($row['total_num']) < 1) {
                    continue;
                }
                $list[$row['user_id']] = $row;
            }
        }
        if ($lists) {
            foreach ($lists as $list2) {
                foreach ($list2 as $row) {
                    if (intval($row['total_num']) < 1) {
                        continue;
                    }
                    $user_id = $row['user_id'];
                    if (isset($list[$user_id])) {
                        $list[$user_id]['total_num'] += $row['total_num'];
                    } else {
                        $list[$user_id] = $row;
                    }
                }
            }
        }
        if ($list) {
            $list = array_values($list);
            usort($list, function($a, $b) {
                $al = intval($a['total_num']);
                $bl = intval($b['total_num']);
                if ($al == $bl)
                    return 0;
                return ($al > $bl) ? -1 : 1;
            });
        }
        return $list;
    }

    public function rm() {

        $GLOBALS['cache']->clear_by_name($this->key);
    }

    public function clear_all() {

        $GLOBALS['cache']->clear_by_name($this->key);
    }

}

?>